#!/usr/bin/env bash
#options get set in ~/.runrc
#defaults get set here

# For bools empty == no; all else == yes
make_cmd_bg="YES";      #BOOL - run process in background mode or not
default_cmd="";        	#TEXT - prefill textbox with this command
dialog_cmd="dialog";	#TEXT - which program to use for dialog (dialog, xdialog, echo for debugging)
extra_opts="YES";		#BOOL - enable XDialog options; only works dialog_cmd is set to XDialog or extra_opts is set to "override" 
force_real_cmd="YES";	#BOOL - fail if program does not exist or is not executible
cmd_path="$PATH";		#TEXT - path to use for executing command;
### next option not active ###
no_run_list="";		#TEXT - if program is in this list don't run it

### Only use these options when running under a graphical dialog system ###
x_screen_placement="screen-center";	#SET (screen-center,under-mouse,auto-placement) - Where does the dialog go
x_buttons_style="default"; 		#SET (default,icon,text) - What type of buttons to display
x_force_program="";			#BOOL - force the program to be non-empty

help_mode="line"				#SET (line,cat) - changes how to display help to the user (line==normal; cat==Xdialog)

### add command to check for existance of program...

#don't read user prefs if env var tells us not to (for scripts...)
if [ -z "$RUNCMD_NO_PREFS" ]
then
	if [ -x ~/.runrc ]
	then
		source ~/.runrc
	fi
fi

# Lets make sure we only use extra_opts when we really want to
if [ "$dialog_cmd" != "Xdialog" -a "$extra_opts" != "OVERRIDE" ]
then
	extra_opts="";
fi
base="$(basename $0)";
helpfile="$(dirname $0)/$base.help.txt";
	
#dialog_params="--title Run;
dialog_params="--title Run";
# --backtitle text shows text above "What command..."

# Are we in a script where we have to run a program (this can be easily subvered by running something like false but lets provide the option anyway)
if [ -z $x_force_program ]
then
	dialog_close_params="--allow-close";
else
	dialog_close_params="--no-close --no-cancel";
fi
dialog_extra_params="$dialog_close_params --$x_screen_placement \
	--no-wrap --stderr --buttons-style $x_buttons_style \
	--ok-label Run
	";
# If we use the extra params then lets add them to the variable
if [ -n "$extra_opts" ]
then
	dialog_params="$dialog_params $dialog_extra_params";
fi

# Lets get a temporary file here to store the program in (I wish we could do this with a named pipe or something like that
TMPFILE=$(mktemp -t $base) || exit 1

if [ "$help_mode" = "line" ]
then
	$dialog_cmd $dialog_params --hline "Press F1 for help" --hfile "$helpfile" \
       --inputbox "What command do you want to run?" 9 40 "$default_cmd" 2>$TMPFILE;
fi
if [ "$help_mode" = "cat" ]
then
	$dialog_cmd $dialog_params --help "$(cat $helpfile)" \
       --inputbox "What command do you want to run?" 0 0 "$default_cmd" 2>$TMPFILE;
fi
program="$(cat $TMPFILE)";
rm $TMPFILE;

if [ -n "$program" ]
then
	# set the path as late as possible because otherwise key programs won't be found
	old_path="$PATH";
	PATH="$cmd_path";
	if [ -n "$force_real_cmd" -a ! -x "$program" ]
	then	
		#we need to return to the original path to continue normal operations
		PATH="$old_path";
		$dialog_cmd --title "Program not executible" --msgbox "The file requested does not exist or can not be executed" 5 62
		if [ -n $x_force_program ]
		then
			source $0;
		fi
	else	
		if [ -z "$make_cmd_bg" ]
		then
			$program;
		else	
			$program &
		fi;
	fi
	#we need to return to the original path to continue normal operations
	PATH="$old_path";
else
	if [ -n $x_force_program ]
	then
		source $0;
	fi
	echo "No program" > /dev/stderr
	exit 1;
fi

exit 0;